package com.example.service.flowable.adm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mapper.EmpMapper;
import com.example.mapper.VehicleMapper;
import com.example.mapper.VehiclePyMapper;
import com.example.mapper.VehicleRecordMapper;
import com.example.pojo.Emp;
import com.example.pojo.Vehicle;
import com.example.pojo.VehiclePy;
import com.example.pojo.VehicleRecord;
import com.example.service.flowable.adm.IVehiclesService;
import com.example.service.impl.VehicleRecordServiceImpl;
import com.example.vo.ResultVo;
import com.example.vo.adm.VehicleVo;
import org.apache.ibatis.annotations.Insert;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IVehiclesServiceImpl implements IVehiclesService  {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private VehiclePyMapper vehiclePyMapper;

    @Autowired
    private TaskService taskService;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private VehicleRecordServiceImpl vehicleRecordService;

    @Autowired
    private VehicleRecordMapper vehicleRecordMapper;

    @Autowired
    private EmpMapper empMapper;

    @Value("${process-definition-id.vehicle}")
    private String processDefinitionId;

    @Override
    public ResultVo addVehicleflowable(VehicleVo vehicleVo) {
        Map<String,Object> variables= new HashMap<>();
        variables.put("manager",vehicleVo.getManager());
        variables.put("brand",vehicleVo.getBrand());
        variables.put("licensePlate",vehicleVo.getLicensePlate());
        variables.put("type",vehicleVo.getType());
        variables.put("vehicleId",vehicleVo.getVehicleId());
        variables.put("empName",vehicleVo.getEmpName());
        variables.put("status",vehicleVo.getStatus());
        variables.put("kaishiTime",vehicleVo.getKaishiTime());
        variables.put("jieshuTime",vehicleVo.getJieshuTime());
        variables.put("admSpecialist",vehicleVo.getAdmSpecialist());
        Authentication.setAuthenticatedUserId(vehicleVo.getEmpId());//设置流程的发起人
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
        //新增审批记录表
        VehiclePy vehiclePy = new VehiclePy();
        BeanUtils.copyProperties(vehicleVo,vehiclePy);
        vehiclePy.setStartTime(vehicleVo.getKaishiTime());
        vehiclePy.setEndTime(vehicleVo.getJieshuTime());
        vehiclePy.setProcessId(processInstance.getId());
        vehiclePy.setEmpId(vehicleVo.getEmpId());
        vehiclePyMapper.insert(vehiclePy);
        return ResultVo.ok();
    }

    @Override
    public ResultVo getAssessingOfficer() {
        //1、行政人员
        List<Emp> list = empMapper.selectList(new QueryWrapper<Emp>().select("id,emp_name").eq("dept_id", 21));
        return ResultVo.response(list);
    }



    @Override
    public ResultVo getMyTask(String empId) {
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(empId)
                .processDefinitionId(processDefinitionId)
                .includeProcessVariables()
                .list();
        ArrayList<VehicleVo> vehicleVos = new ArrayList<>();
        list.forEach(e->{
            Map<String, Object> processVariables = e.getProcessVariables();
            VehicleVo vehicleVo = new VehicleVo();
            vehicleVo.setApplicDate(e.getCreateTime())
                    .setBrand((String) processVariables.get("brand"))
                    .setLicensePlate((String)processVariables.get("licensePlate"))
                    .setEmpName((String)processVariables.get("empName"))
                    .setType((String)processVariables.get("type"))
                    .setVehicleId((Integer) processVariables.get("vehicleId"))
                    .setKaishiTime((Date)processVariables.get("kaishiTime"))
                    .setJieshuTime((Date)processVariables.get("jieshuTime"))
                    .setTaskId(e.getId())
                    .setProcessInstanceId(e.getProcessInstanceId());
            vehicleVos.add(vehicleVo);
        });
        return ResultVo.response(vehicleVos);
    }

    @Override
    public ResultVo doAudit(String taskId, Boolean approved, String reply, String licensePlate, String processInstanceId) {
        Map<String, Object> map = new HashMap<>();
        map.put("approved",approved);
        map.put("reply",reply);
        if(approved){
            //2、改变车辆状态
            Vehicle vehicle = new Vehicle()
                    .setLicensePlate(licensePlate)
                    .setEnabled(2);
            vehicleMapper.update(vehicle,new UpdateWrapper<Vehicle>().eq("license_plate",vehicle.getLicensePlate()));
            //3、修改申请表记录
            VehiclePy vehiclePy = vehiclePyMapper.selectOne(new QueryWrapper<VehiclePy>().eq("process_id", processInstanceId));
            vehiclePy.setStatus(1);
            vehiclePyMapper.updateById(vehiclePy);
            VehicleRecord vehicleRecord = new VehicleRecord().setJieshuTime(vehiclePy.getEndTime())
                    .setKaishiTime(vehiclePy.getStartTime())
                    .setJieshuTime(vehiclePy.getEndTime())
                    .setVehiclePyId(vehiclePy.getId());
            vehicleRecordMapper.insert(vehicleRecord);
        }
        taskService.complete(taskId,map);
        return ResultVo.ok();
    }

    @Override
    public ResultVo getAllPy() {
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionId(processDefinitionId)
                .includeProcessVariables()
                .list();
        ArrayList<VehicleVo> vehicleVos = new ArrayList<>();
        list.forEach(e->{
            Map<String, Object> processVariables = e.getProcessVariables();
            VehicleVo vehicleVo = new VehicleVo();
            vehicleVo.setApplicDate(e.getCreateTime())
                    .setBrand((String) processVariables.get("brand"))
                    .setLicensePlate((String)processVariables.get("licensePlate"))
                    .setEmpName((String)processVariables.get("empName"))
                    .setType((String)processVariables.get("type"))
                    .setVehicleId((Integer) processVariables.get("vehicleId"))
                    .setKaishiTime((Date)processVariables.get("kaishiTime"))
                    .setJieshuTime((Date)processVariables.get("jieshuTime"));
//                    .setApproved((Boolean)processVariables.get("approved"));
            vehicleVos.add(vehicleVo);
        });
        return ResultVo.response(vehicleVos);
    }
}
